<?php

namespace App\Http\Requests;
use Illuminate\Validation\Rule;

/**
 * 表单验证
 * Class CodeSaveRequest
 * @package App\Http\Requests
 */
class CodeSaveRequest extends BaseRequest
{

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $id = $this->get('id', 0);
        $rules = [
            'code' => [
                'required',
                'between:2,50',
                'regex:/^[a-zA-Z0-9]{2,20}$/',
                Rule::unique('codes', 'code')->where('deleted_at', 0)->ignore($id)
            ],
            'name' => [
                'required',
                'between:2,20'
            ],
            'desc' => [
                'max:250'
            ],
        ];;
        return $rules;
    }

    public function messages()
    {
        return [
            'desc.max' => '备注不超过:max 个字符',
            'name.required' => '请输入名称',
            'name.between' => '名称字符长度:min-:max',
            'code.required' => '请输入代码编号',
            'code.regex' => '代码编号格式错误必须全是字母和数字',
            'code.between' => '代码编号字符长度:min-:max',
            'code.unique' => '代码编号已存在'
        ];
    }
}
